﻿using BAL;
using DAL;
using DAL.DataProvider;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Module.QLHoaDon
{
    public partial class frmQLHoaDon : Form
    {
        public frmQLHoaDon()
        {
            InitializeComponent();
            //this.Height = 1000;
        }

        HoaDonBAO hdBAO = new HoaDonBAO();
        private void frmQLHoaDon_Load(object sender, EventArgs e)
        {
            dgvHangHoa.DataSource = hdBAO.SelectAllHoaDon();
            TextboxInputVisible(false);
            ButtonVisible(true);
            ResetTextboxInput();
        }

        private void dgvHangHoa_RowClicked(object sender, DataGridViewCellEventArgs e)
        {
            
        }



        public void GetDataFromSelectedRow(DataGridViewSelectedRowCollection row)
        {
            txtMaHD.Text = row[0].Cells[0].Value.ToString();
            txtMaKH.Text = row[0].Cells[1].Value.ToString();
            dtpkNgayIn.Value = Convert.ToDateTime(row[0].Cells[2].Value.ToString());
            dtpkNgayMua.Value = Convert.ToDateTime(row[0].Cells[3].Value.ToString());
            txtTienKhach.Text = row[0].Cells[4].Value.ToString();
            txtNVTT.Text = row[0].Cells[5].Value.ToString();
            txtMaQT.Text = row[0].Cells[6].Value.ToString();
            txtGGKhach.Text = row[0].Cells[7].Value.ToString();
        }

        private void dgvHangHoa_Click(object sender, EventArgs e)
        {
            TextboxInputVisible(true);
            txtMaHD.ReadOnly = true;
            txtGGKhach.ReadOnly = true;
            ButtonVisible(false);
            btnUpdateHoaDon.Enabled = true;
            if (dgvHangHoa.SelectedRows.Count >= 1)
            {
                GetDataFromSelectedRow(dgvHangHoa.SelectedRows);
            }

        }

        public HoaDon RowToHoaDon()
        {
            HoaDon obj = new HoaDon();
            obj.maHoaDon = dgvHangHoa.SelectedRows[0].Cells[0].Value.ToString();
            obj.maKhachHang= dgvHangHoa.SelectedRows[0].Cells[1].Value.ToString();
            obj.ngayMuaHang= Convert.ToDateTime(dgvHangHoa.SelectedRows[0].Cells[2].Value.ToString());
            obj.ngayInHoaDon= Convert.ToDateTime(dgvHangHoa.SelectedRows[0].Cells[3].Value.ToString());
            obj.tienKhachDua= Convert.ToInt32(dgvHangHoa.SelectedRows[0].Cells[4].Value.ToString());
            obj.maNVThanhToan= dgvHangHoa.SelectedRows[0].Cells[5].Value.ToString();
            obj.maQT= dgvHangHoa.SelectedRows[0].Cells[6].Value.ToString();
            obj.giamGiaKhach= dgvHangHoa.SelectedRows[0].Cells[7].Value.ToString();
            return obj;
        }


        private void dgvHangHoa_DoubleClick(object sender, EventArgs e)
        {
            HoaDon passData = RowToHoaDon();
            frmChiTietHoaDon frm = new frmChiTietHoaDon(passData);
            frm.ShowDialog();
        }

        private void btnDetailsHoaDon_Click(object sender, EventArgs e)
        {
            HoaDon passData = RowToHoaDon();
            frmChiTietHoaDon frm = new frmChiTietHoaDon(passData);
            frm.ShowDialog();
        }



        HoaDon obj;
        KhachHangBAO khBAO = new KhachHangBAO();
        NhanVienBAO nvBAO = new NhanVienBAO();
        HoaDonDAO hdDAO = new HoaDonDAO();
        private void btnAddHoaDon_Click(object sender, EventArgs e)
        {
            obj = new HoaDon();
            bool flag = false;
            obj.maHoaDon = txtMaHD.Text;
            try
            {
                List<KhachHang> listKH = khBAO.KhachHang_GetData();
                foreach (KhachHang itemKH in listKH)
                    if (itemKH.maKhachHang == txtMaKH.Text)
                        flag = true;
                if (!flag)
                    throw new Exception("loi ma kh");
                obj.maKhachHang = txtMaKH.Text;
                flag = false;
                try
                {
                    List<NhanVien> listNV = nvBAO.GetListNhanVien();
                    foreach (NhanVien itemNV in listNV)
                        if (itemNV.maNV == txtNVTT.Text)
                            flag = true;
                    if (!flag)
                        throw new Exception("loi thu ngan");
                    flag = false;
                    obj.maNVThanhToan = txtNVTT.Text;
                    obj.maQT = txtMaQT.Text;
                    obj.ngayMuaHang = dtpkNgayMua.Value;
                    obj.ngayInHoaDon = dtpkNgayMua.Value;
                    obj.tienKhachDua = Convert.ToInt32(txtTienKhach.Text);
                    obj.giamGiaKhach = "NULL";
                    hdDAO.InsertHoaDon(obj);
                    frmChiTietHoaDon frm = new frmChiTietHoaDon(obj);
                    frm.ShowDialog();
                    ResetTextboxInput();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    txtNVTT.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                txtMaKH.Focus();
            }

            dgvHangHoa.DataSource = hdBAO.SelectAllHoaDon();
        }

        private void TextboxInputVisible(bool logic)
        {
            txtGGKhach.ReadOnly = !logic;
            txtMaHD.ReadOnly = !logic;
            txtMaKH.ReadOnly = logic;
            txtMaQT.ReadOnly = logic;
            txtNVTT.ReadOnly = logic;
            txtTienKhach.ReadOnly = logic;
        }

        private void ButtonVisible(bool logic)
        {
            btnAddHoaDon.Enabled = logic;
            btnDetailsHoaDon.Enabled = !logic;
            btnUpdateHoaDon.Enabled = logic;
        }

        private void ResetTextboxInput()
        {
            txtGGKhach.Text = "";
            txtMaHD.Text = "";
            txtMaKH.Text = "";
            txtMaQT.Text = "";
            txtNVTT.Text = "";
            txtTienKhach.Text = "";
            dtpkNgayIn.Value = dtpkNgayMua.Value = DateTime.Today;

            string maHDMoi = "HD";
            int STT = Convert.ToInt32(dgvHangHoa.Rows.Count) + 1;
            maHDMoi += STT.ToString();
            txtMaHD.Text = maHDMoi;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetTextboxInput();
            ButtonVisible(true);
            TextboxInputVisible(false);
        }

        private void btnUpdateHoaDon_Click(object sender, EventArgs e)
        {

        }

        
    }
}
